package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.constant.MessageConstant;
import com.mfl.context.BaseContext;
import com.mfl.exception.BookAlreadyExistsException;
import com.mfl.mapper.CollectMapper;
import com.mfl.mapper.CourseCategoryMapper;
import com.mfl.mapper.KYCourseBookMapper;
import com.mfl.pojo.dto.KYCourseBookDTO;
import com.mfl.pojo.dto.KYCourseBookPageQueryDTO;
import com.mfl.pojo.entity.Collect;
import com.mfl.pojo.entity.CourseCategory;
import com.mfl.pojo.entity.KYCourseBook;
import com.mfl.pojo.vo.CourseCategoryVO;
import com.mfl.pojo.vo.KYCourseBookPageQueryVO;
import com.mfl.pojo.vo.KYCourseBookVO;
import com.mfl.pojo.vo.KYCourseVideoVO;
import com.mfl.service.KYCourseBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class KYCourseBookServiceImpl implements KYCourseBookService {

    @Autowired
    private KYCourseBookMapper kyCourseBookMapper;
    @Autowired
    private CourseCategoryMapper courseCategoryMapper;
    @Autowired
    private CollectMapper collectMapper;

    /**
     * 分页查询课程书籍
     *
     * @param kyCourseBookPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(KYCourseBookPageQueryDTO kyCourseBookPageQueryDTO) {
        PageHelper.startPage(kyCourseBookPageQueryDTO.getPage(), kyCourseBookPageQueryDTO.getPageSize());
        Page<KYCourseBookPageQueryVO> page = kyCourseBookMapper.queryByPage(kyCourseBookPageQueryDTO);

        page.getResult().forEach(item -> {
            // 把书籍分类id转换成书籍分类名称
            CourseCategory coursecategory = courseCategoryMapper.getByCourseCategoryId(item.getCategoryId());
            item.setCourseCategory(coursecategory.getCourseCategoryName());
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增课程书籍
     *
     * @param kyCourseBookDTO
     */
    @Override
    public void insert(KYCourseBookDTO kyCourseBookDTO) {
        // 先查询是否有该课程书籍
        KYCourseBook kyCourseBook = kyCourseBookMapper.getBySourceName(kyCourseBookDTO.getBookName());
        if (kyCourseBook != null) {
            throw new BookAlreadyExistsException(MessageConstant.BOOK_ALREADY_EXISTS);
        }
        KYCourseBook build = KYCourseBook.builder()
                .bookName(kyCourseBookDTO.getBookName())
                .categoryId(kyCourseBookDTO.getCategoryId())
                .cover(kyCourseBookDTO.getCover())
                .bookFile(kyCourseBookDTO.getBookFile())
                .createUser(BaseContext.getCurrentId())
                .createTime(LocalDateTime.now())
                .build();
        kyCourseBookMapper.insert(build);
    }

    /**
     * 批量删除课程书籍
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        kyCourseBookMapper.deleteByIds(ids);
    }

    /**
     * 根据id查询课程书籍
     *
     * @param id
     * @return
     */
    @Override
    public KYCourseBookVO getCourseBookById(int id) {
        KYCourseBook kyCourseBook = kyCourseBookMapper.getCourseBookById(id);
        // 根据课程类型id查询类型
        CourseCategory courseCategory = courseCategoryMapper.getByCourseCategoryId(kyCourseBook.getCategoryId());
        // 查询收藏状态
        Collect collect = collectMapper.selectByUserIdAndBookId(BaseContext.getCurrentId(), id, "book");
        // 统计收藏次数
        int collectNum = collectMapper.selectCount(id, "book");
        return KYCourseBookVO.builder()
                .id(kyCourseBook.getId())
                .bookName(kyCourseBook.getBookName())
                .categoryId(kyCourseBook.getCategoryId())
                .cover(kyCourseBook.getCover())
                .bookFile(kyCourseBook.getBookFile())
                .isCollect(collect != null ? 1 : 0)
                .collectNum(collectNum)
                .createTime(kyCourseBook.getCreateTime())
                .courseCategory(courseCategory.getCourseCategoryName())
                .build();
    }

    /**
     * 修改课程书籍
     *
     * @param kyCourseBookDTO
     */
    @Override
    public void update(KYCourseBookDTO kyCourseBookDTO) {
        KYCourseBook kyCourseBook = KYCourseBook.builder()
                .id(kyCourseBookDTO.getId())
                .bookName(kyCourseBookDTO.getBookName())
                .categoryId(kyCourseBookDTO.getCategoryId())
                .cover(kyCourseBookDTO.getCover())
                .bookFile(kyCourseBookDTO.getBookFile())
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();
        kyCourseBookMapper.update(kyCourseBook);
    }
}
